const BookModel = require('../db/models/allBook');

class BookService {
    //爬取书本
    async addAllBookData (data) {
        const bid = data.bid;

        const result = await BookModel.findOne({
            where: { bid }
        })

        if (result) {
            return await BookModel.update(data, {
                where: { bid }
            })
        } else {
            return await BookModel.create(data);
        }
    }

    //查询所有书本
    async getBookData () {
        return await BookModel.findAll({
            attributes: {
                exclude: ['imgUrl', 'createdAt', 'updatedAt']
            }
        });
    }

    //修改书本分类
    async changeField (bid, field) {
        const ret = await BookModel.update({ field: field }, {
            where: { bid }
        })

        return ret[0];
    }

    //修改书本上下架状态
    async changeBookStatus (id, status) {
        const ret = await BookModel.update({ status }, {
            where: { bid: id }
        });

        return ret[0];
    }
}

module.exports = new BookService();